import uuid
from django.db.models import Q
from rest_framework.response import Response
from apis.app01.serializers import GoodsSer
from .models import Posts, Comment, Like, Goods, Vip, Promo_Code
from apis.user.models import User

def posts_data(page,size,sort,type):
    data_start = (int(page) - 1) * int(size)  # 计算从哪里切
    data_end = int(page) * int(size)  # 切到哪里
    queryset = Posts.objects.filter(status=0)[data_start:data_end]  # 0默认是没有删除
    li = []
    for i in queryset:
        print(i.uid)
        data = {}
        user = User.objects.filter(id=i.uid).first()  # 状态查询
        data['nickname'] = user.nickname
        data['img'] = user.img
        data['id'] = user.id
        data['content'] = i.content
        data['title'] = i.title
        data['collect_nums'] = i.collect_nums
        data['like_nums'] = i.like_nums
        data['create_time'] = i.create_time
        li.append(data)
    count = Posts.objects.count()  # 总商品个数
    return Response({"error_code": 200, "message": "信息返回成功", "data": li, "count": count})


def create_posts(data,uid):
    type = data.get("type")
    title = data.get("title")
    content = data.get("content")  # 内容验证:TODO:色情等内容的验证
    if not uid:
        return Response({"error_code": 400, "message": "token已失效"})
    try:
        Posts.objects.create(type=type, content=content, title=title, uid=uid)
        return Response({"error_code": 200, "message": "信息插入成功"})
    except Exception as e:
        print(e)
        return Response({"error_code": 404, "message": "信息插入失败"})

def get_user(uid):
    user = User.objects.filter(Q(status=1) | Q(status=2), id=uid).first()
    if user.status == 2:  # 2代表不愿被展示
        return Response({"error_code": 400, "message": "用户不愿意展示数据"})
    if user.status == 1:
        data = {}
        data['id'] = user.id
        data['nickname'] = user.nickname
        data['img'] = user.img
        data['posts'] = []
        posts = Posts.objects.filter(uid=user.id)
        posts_li = {}
        for i in posts:
            posts_li['pid'] = i.id
            posts_li['title'] = i.title
            data['posts'].append(posts_li)
        return Response({"error_code": 200, "message": "数据查询成功", "data": data})

def posts_detail(pid):
    posts = Posts.objects.get(id=pid, status=0).first()  # 帖子的信息
    data = {}
    data['title'] = posts.title
    data['content'] = posts.content
    data['title'] = posts.title
    data['collect_nums'] = posts.collect_nums
    data['like_nums'] = posts.like_nums
    data['create_time'] = posts.create_time
    user = User.objects.filter(Q(status=1) | Q(status=2), id=posts.uid)  # 帖子用户的数据
    data['nickname'] = user.nickname
    data['uid'] = posts.uid
    data['img'] = user.img
    data['comment'] = []
    comment = Comment.objects.filter(p_id=pid, father_id="")  # status=0后期调优 status代表的含义
    for i in comment:
        comment = {}
        comment['content'] = i.content
        comment['author_id'] = i.auther_id
        data['comment'].append(comment)
    return Response({"error_code": 200, "message": "数据获取成功", "data": data})



def comment_detail(p_id,id):
    if not p_id:
        return Response({"error_code": 400, "message": "数据不全"})
    if not id:
        mes = Comment.objects.filter(father_id=0, p_id=p_id, status=0)
        print(mes)
        # response.content = '成功'
        res_list = []
        for i in mes:
            res = {}
            auther = User.objects.filter(Q(status=1) | Q(status=2), id=i.auther_id).filter()
            res['id'] = i.id
            res['content'] = i.content
            res['auther_name'] = auther.nickname  # 回复人的昵称
            res['auther_img'] = auther.img  # 回复人的图片
            res['auther_id'] = i.auther_id  # 回复人的uid
            res['creat_time'] = i.create_time
            res_list.append(res)
        return Response({"error_code": 200, "message": "评论数据返回成功", "data": res_list})
    else:
        res_list = []
        mes = Comment.objects.filter(status=0, father_id=id, p_id=p_id).order_by('create_time')  # 0代表没删除1代表已删除
        for i in mes:
            huifu = User.objects.filter(Q(status=1) | Q(status=2), id=i.huifu_id).first()
            auther = User.objects.filter(Q(status=1) | Q(status=2), id=i.auther_id).filter()
            res = {}
            res['huifu_name'] = huifu.nickname  # 被回复人昵称
            res['huifu_img'] = huifu.img  # 被回复人图片
            res['huifu_id'] = mes.huifu_id
            res['auther_name'] = auther.nickname  # 回复人的昵称
            res['auther_img'] = auther.img  # 回复人的图片
            res['auther_id'] = mes.auther.id
            res['mes_content'] = mes.content  # 回复内容
            res['mes_create_time'] = mes.create_time  #
            res_list.append(res)
        return Response({"error_code": 200, "message": "回复数据返回成功", "data": res_list})

def delete_posts(pid,uid):
    if not uid:
        return Response({"error_code": 400, "message": "token已失效"})
    try:
        posts = Posts.objects.filter(id=pid, status=0, uid=uid).update(status=1)  # 帖子逻辑删除
        comment = Comment.objects.filter(p_id=pid).update(status=1)  # 所有对应的评论也被删除
        return Response({"error_code": 200, "message": "你的帖子已被删除"})
    except Exception as e:
        print(e)
        return Response({"error_code": 400, "message": "权限不够或者你的帖子已删除"})



def like_posts(pid,uid):
    if not uid:
        return Response({"error_code": 400, "message": "token已失效"})
    if not pid:
        return Response({"error_code": 400, "message": "帖子的唯一参数缺失"})
    like = Like.objects.create(uid=uid, p_id=pid)
    posts = Posts.objects.filter(id=pid, status=0).first()
    try:
        if like.status == 0:
            like.status = 1
            like.save()
            posts.like_nums += 1
            posts.save()
            return ({"error_code": 200, "message": "点赞成功"})
        else:
            like.status = 0
            like.save()
            posts.like_nums -= 1
            posts.save()
            return ({"error_code": 200, "message": "取消点赞"})
    except Exception as e:
        return ({"error_code": 404, "message": "点赞失败"})


def collect_posts(pid,uid):
    if not uid:
        return Response({"error_code": 400, "message": "token已失效"})
    if not pid:
        return Response({"error_code": 400, "message": "帖子的唯一参数缺失"})
    like = Like.objects.create(uid=uid, p_id=pid)
    posts = Posts.objects.filter(id=pid, status=0).first()
    try:
        if like.status == 0:
            like.status = 1
            like.save()
            posts.collect_nums += 1
            posts.save()
            return ({"error_code": 200, "message": "收藏成功"})
        else:
            like.status = 0
            like.save()
            posts.collect_nums -= 1
            posts.save()
            return ({"error_code": 200, "message": "取消收藏"})
    except Exception as e:
        return ({"error_code": 404, "message": "收藏失败"})

def goods_list(category_id,page,size,desc):
    if desc == 0:
        type = 'create_time'
    if desc == 1:
        type = 'cattention_nums'
    if desc == 2:
        type = 'learn_nums'
    data_start = (int(page) - 1) * int(size)  # 从哪里开始切
    data_end = int(page) * int(size)  # 切到哪里
    count = Goods.objects.filter(status=0).count()
    if category_id == 0:
        goods = Goods.objects.filter(status=0).order_by(type)[data_start:data_end]
    else:
        goods = Goods.objects.filter(status=0, category_id=category_id).order_by(type)[data_start:data_end]
    ser = GoodsSer(goods, many=True)
    return Response({"error_code": 200, "message": "数据获取成功", "data": ser.data, "count": count})


def promo(uid):
    if not uid:
        return Response({"error_code": 400, "message": "token已失效"})
    vip = Vip.objects.filter(uid=uid).first()
    if vip.status > 0:
        name = uuid.uuid1().hex
    else:
        return Response({"error_code": 400, "message": "你没有生成优惠码的权限"})
    try:
        Promo_Code.objects.create(name=name, uid=uid)
        vip.promo_num -= 1
        vip.save()
        return Response({"error_code": 200, "message": "优惠码生成成功", "promo_code": name})
    except:
        return Response({"error_code": 404, "message": "获取失败，请重试"})



